<template>
  <el-dialog
    @close="qrkssgCancel('qrkssgRuleForm')"
    :close-on-click-modal="false"
    title="确认开始施工"
    :visible.sync="qrkssgVisible"
    width="600px"
  >
    <div class="scroll-view">
      <el-form
        :model="qrkssgRuleForm"
        :rules="qrkssgRules"
        ref="qrkssgRuleForm"
        label-position="top"
      >
        <el-form-item label="开工日期" prop="start_date">
          <el-date-picker
            class="input-box"
            v-model="qrkssgRuleForm.start_date"
            type="date"
            clearable
            :picker-options="pickerOptionsEnd"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工长" prop="foreman_id">
          <el-select
            class="input-box"
            v-model="qrkssgRuleForm.foreman_id"
            placeholder="请选择"
            filterable
            clearable
            @change="qrkssgChangeFunc"
          >
            <el-option
              v-for="item in foremanList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开工文件 (.jpg,.png,.gif)" prop="start_file">
          <el-input
            style="position: absolute; z-index: 0; top: -999999px"
            v-model="qrkssgRuleForm.start_file.length"
          ></el-input>
          <el-upload
            :on-change="
              (file, fileList) => {
                tjyfkpzChangeFile(file, fileList, 'start_file');
              }
            "
            :on-remove="
              (file, fileList) => {
                tjyfkpzFileRemove(file, fileList, 'start_file');
              }
            "
            :auto-upload="false"
            :file-list="qrkssgRuleForm.start_file"
            action="#"
            accept="image/*"
          >
            <el-button size="small" icon="el-icon-plus">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            class="input-box"
            v-model="qrkssgRuleForm.remark"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button
        type="primary"
        @click="qrkssgSubmit('qrkssgRuleForm')"
        :disabled="diaLoading"
        :loading="diaLoading"
        >提交</el-button
      >
      <el-button @click="qrkssgCancel('qrkssgRuleForm')">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    var start_file = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("开工文件"));
      } else {
        callback();
      }
    };
    return {
      diaLoading: false,
      qrkssgVisible: false,
      qrkssgRuleForm: {
        start_date: "",
        foreman_id: "",
        foreman_name: "",
        remark: "",
        start_file: [],
      },
      qrkssgRules: {
        start_date: [
          { required: true, message: "请选择开工日期", trigger: "change" },
        ],
        foreman_id: [
          { required: true, message: "请选择工长", trigger: "change" },
        ],
        remark: "",
        start_file: {
          required: true,
          validator: start_file,
          trigger: "change",
        },
      },
      foremanList: [], //工长列表
      pickerOptionsStart: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      pickerOptionsEnd: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
      },
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      if (this.addDialogVisible) {
        const now = new Date();
        const year = now.getFullYear();
        const month = now.getMonth() + 1; // 月份从0开始，需要加1
        const day = now.getDate();
        console.log(year, month, day);
        qrkssgRuleForm.start_date = `${year}-${month}-${day}`;
        this.qrkssgRuleForm = qrkssgRuleForm;
        this.getForemanList();
      }
      this.qrkssgVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() {},
  methods: {
    // 开始施工确认
    qrkssgCancel(formName) {
      this.qrkssgRuleForm = {
        start_date: "",
        foreman_id: "",
        foreman_name: "",
        remark: "",
        start_file: [],
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    qrkssgSubmit(formName) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      console.log(qrkssgRuleForm, "开始施工确认参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              project_id: this.project_id,
              start_date: qrkssgRuleForm.start_date,
              foreman_id: qrkssgRuleForm.foreman_id,
              foreman_name: qrkssgRuleForm.foreman_name,
              remark: qrkssgRuleForm.remark,
            })
          );
          qrkssgRuleForm.start_file.forEach((item) => {
            formdata.append("start_file[]", item.raw);
          });
          projectApi
            .onSiteStartup(formdata)
            .then((res) => {
              console.log(res, "开始施工确认");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getForemanList() {
      foremanApi
        .getForemanList({
          page: 1,
          num: 999,
          show_deactivate: 2,
        })
        .then((res) => {
          console.log(res, "工长列表");
          if (res.code == 200) {
            this.foremanList = res.data.list;
          }
        })
        .catch((err) => {});
    },
    qrkssgChangeFunc(e) {
      let foremanList = this.foremanList;
      let qrkssgRuleForm = this.qrkssgRuleForm;
      let index = foremanList.findIndex((item) => {
        return item.id == e;
      });
      qrkssgRuleForm.foreman_name = foremanList[index].name;
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
    tjyfkpzChangeFile(file, list, type) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      qrkssgRuleForm.start_file = [file];
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
    tjyfkpzFileRemove(file, list, type) {
      let qrkssgRuleForm = this.qrkssgRuleForm;
      qrkssgRuleForm.start_file = list;
      this.qrkssgRuleForm = qrkssgRuleForm;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>